package com.eascs.spider.dsk.db;

import java.util.Set;

import com.eascs.spider.dsk.util.PropertiesUtil;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * 基于redis实现队列
 * @author hzp
 * @date 2018-04-17
 */
public class RedisSession {

	private JedisPool pool = null;
	private static RedisSession session;
	
	public synchronized static RedisSession getIntance(){
		return session == null? session = new RedisSession():session;
	}
	private RedisSession() {
		if (pool == null) {
            JedisPoolConfig config = new JedisPoolConfig();
            config.setMaxTotal(1000);
            config.setMaxIdle(5);
            config.setMaxWaitMillis(1000*100);
            config.setTestOnBorrow(true);
            pool = new JedisPool(config, 
            					PropertiesUtil.getConfig("redis.ip"), 
            					Integer.valueOf(PropertiesUtil.getConfig("redis.port")), 
            					1000*100, 
            					PropertiesUtil.getConfig("redis.password"),
            					Integer.valueOf(PropertiesUtil.getConfig("redis.db")));

          //pool = new JedisPool(config, "192.168.14.135", 6379);
        }
	}
    
	//1.1，往指定Set集合插入key，用于去重
	public void sadd(String collectionName , String key){
	    Jedis jedis = pool.getResource();
	    jedis.sadd(collectionName, key);
	    returnJedis(jedis);
    }
	
	//1.2，从指定集合Set检查是否存在key，用于去重
	public boolean sexists(String collectionName , String key){
	    Jedis jedis = pool.getResource();
	    boolean isNumber = jedis.sismember(collectionName, key);
	    returnJedis(jedis);
	    return isNumber;
    }
	
    //2.1，从队列左边排头插入值
    public void lpush(String key, String value) {
    	Jedis jedis = pool.getResource();
    	jedis.lpush(key ,value);
		returnJedis(jedis);
    }
    
    //2.2，从队列右边队尾取值
    public String rpop(String queue) {
    	Jedis jedis = pool.getResource();
		String value = jedis.rpop(queue);
		returnJedis(jedis);
		return value;
    }    
    
    private void returnJedis(Jedis jedis){
    	pool.returnBrokenResource(jedis);
    }
     
    //3.1，hash处理，往指定集合存入K,V
    public void hset(String collectionName , String key , String value){
    	Jedis jedis = pool.getResource();
    	jedis.hset(collectionName, key, value);
    	returnJedis(jedis);
    }
    
   //3.2，hash处理，从指定集合根据键取值
    public String hget(String collectionName , String key){
    	Jedis jedis = pool.getResource();
    	String value = jedis.hget(collectionName, key);
    	returnJedis(jedis);
    	return value;
    }
    
    //3.3，hash处理，从指定集合取所有键
    public Set<String> hkeys(String collectionName){
    	Jedis jedis = pool.getResource();
    	Set<String> keys = jedis.hkeys(collectionName);
    	returnJedis(jedis);
    	return keys;
    }

    //3.4，检查指定集合是否存在key，用于去重
    public boolean hexists(String collectionName , String key){
	    Jedis jedis = pool.getResource();
	    boolean result = jedis.hexists(collectionName, key);
	    returnJedis(jedis);
	    return result;
    }
    
    public static void main(String[] args) {
    	RedisSession session = new RedisSession();
    	session.hset("test1", "111111", "sdfdsf");
	}
}
